package com.example.xianji.utils;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.example.xianji.XJApplication;
import com.example.xianji.activity.ShowWebImageActivity;

/**
 * 和h5交互 的回调
 *
 * @author me
 */

public class Javascript {
    private Context context;
    private String Tag = "/XianJiNews/";
    private String[] pic;

    public Javascript(Context context) {
        this.context = context;
    }

    @JavascriptInterface
    // 这个注解很重要
    public void openImage(String img, String arr[], int index) {
        System.out.println(img);
        for (int i = 0; i < arr.length; i++) {
            if (!new File(SaveSdk.getImagePath(arr[i], Tag, context.getApplicationContext())).exists()) {
                pic[i] = arr[i];//http路径
            } else {
                pic[i] = SaveSdk.getImagePath(arr[i], Tag, context.getApplicationContext());//内存路径
            }
        }
        Intent intent = new Intent();


        intent.putExtra("picture", pic);

        intent.putExtra("index", index);
        Log.i("222222222222img", pic[0]);
        Log.i("索引缩影所用", index + "");
        intent.setClass(context, ShowWebImageActivity.class);
        context.startActivity(intent);

        System.out.println(img);
    }

    /**
     * 遍历数组存到SD卡中
     *
     * @param arr
     */
    @JavascriptInterface
    // 这个注解很重要
    public void DownLoadImage(final String arr[]) {

        pic = new String[arr.length];

        for (int i = 0; i < arr.length; i++) {
            Log.i("下载下载下载", arr[i] + "下载下载下载");
            /**
             * 保存到SDK中
             */
            final int a = i;
            if (!new File(SaveSdk.getImagePath(arr[i], Tag, context)).exists()) {
                XJApplication.cachThreadPool.execute(new Runnable() {

                    @Override
                    public void run() {
                        SaveSdk.downloadImage(arr[a], Tag, context);
                    }
                });
            }

            /*
             * pic[i] = SaveSdk.getImagePath(arr[i], Tag, context);
             */
        }

    }
}
